// 枚举类型
export enum PropertyType {
  STREET = 'STREET',
  RAILROAD = 'RAILROAD',
  UTILITY = 'UTILITY',
}

export enum SpaceType {
  PROPERTY = 'PROPERTY',
  CHANCE = 'CHANCE',
  COMMUNITY = 'COMMUNITY',
  GO = 'GO',
  JAIL = 'JAIL',
  FREE_PARKING = 'FREE_PARKING',
  GO_TO_JAIL = 'GO_TO_JAIL',
  TAX = 'TAX',
}

export enum GamePhase {
  SETUP = 'SETUP',
  CHARACTER_SELECT = 'CHARACTER_SELECT',
  PLAYING = 'PLAYING',
  GAME_OVER = 'GAME_OVER',
}

export enum AbilityType {
  PASSIVE = 'passive',
  ACTIVE = 'active',
}

export enum TradeStatus {
  PENDING = 'pending',
  ACCEPTED = 'accepted',
  REJECTED = 'rejected',
}

export enum GameErrorType {
  INVALID_MOVE = 'INVALID_MOVE',
  INSUFFICIENT_FUNDS = 'INSUFFICIENT_FUNDS',
  INVALID_TRADE = 'INVALID_TRADE',
  PROPERTY_NOT_AVAILABLE = 'PROPERTY_NOT_AVAILABLE',
  ABILITY_ON_COOLDOWN = 'ABILITY_ON_COOLDOWN',
  INVALID_GAME_STATE = 'INVALID_GAME_STATE',
  SAVE_LOAD_ERROR = 'SAVE_LOAD_ERROR',
  NETWORK_ERROR = 'NETWORK_ERROR',
}

// 游戏上下文接口
export interface GameContext {
  gameState: GameState;
  currentPlayer: Player;
  targetPlayer?: Player;
  property?: Property;
  amount?: number;
}

// 能力接口
export interface Ability {
  id: string;
  name: string;
  description: string;
  type: AbilityType;
  effect: (context: GameContext) => void;
  cooldownTurns?: number;
}

// 角色接口
export interface Character {
  id: string;
  name: string;
  description: string;
  avatar: string;
  passiveAbility: Ability;
  activeAbility: Ability;
}

// 道具接口
export interface Item {
  id: string;
  name: string;
  description: string;
  effect: (context: GameContext) => void;
}

// 地块接口
export interface Property {
  id: string;
  name: string;
  position: number;
  type: PropertyType;
  price: number;
  rent: number[];
  colorGroup: string;
  owner: string | null;
  level: number;
  mortgaged: boolean;
  upgradeCost: number;
}

// 玩家接口
export interface Player {
  id: string;
  name: string;
  character: Character;
  position: number;
  money: number;
  properties: string[]; // Property IDs
  items: Item[];
  isActive: boolean;
  isBankrupt: boolean;
  abilityLastUsed: number; // Turn number when ability was last used
  canReroll: boolean; // For Lucky Charm passive
}

// 事件选择接口
export interface Choice {
  text: string;
  effect: (player: Player, game: GameState) => void;
}

// 事件卡接口
export interface EventCard {
  id: string;
  title: string;
  description: string;
  type: 'chance' | 'community';
  effect: (player: Player, game: GameState) => void;
  choices?: Choice[];
}

// 棋盘空间接口
export interface BoardSpace {
  position: number;
  type: SpaceType;
  property?: Property;
  name: string;
  action?: (player: Player, game: GameState) => void;
}

// 交易物品接口
export interface TradeItem {
  type: 'property' | 'money' | 'item';
  id?: string; // For property or item
  amount?: number; // For money
}

// 交易接口
export interface TradeOffer {
  id: string;
  from: string;
  to: string;
  offering: TradeItem[];
  requesting: TradeItem[];
  status: TradeStatus;
  timestamp: number;
}

// 游戏事件接口
export interface GameEvent {
  id: string;
  type: string;
  playerId: string;
  description: string;
  timestamp: number;
  data?: unknown;
}

// 通知接口
export interface Notification {
  id: string;
  type: 'info' | 'success' | 'warning' | 'error';
  message: string;
  timestamp: number;
}

// 游戏配置接口
export interface GameConfig {
  playerCount: number;
  initialMoney: number;
  startBonus: number;
  theme: string;
  winCondition: 'lastStanding' | 'targetAssets' | 'timeLimit';
  targetAssets?: number;
  timeLimit?: number; // in turns
  isSinglePlayer: boolean;
}

// 游戏状态接口
export interface GameState {
  id: string;
  players: Player[];
  board: BoardSpace[];
  currentPlayerIndex: number;
  turn: number;
  phase: GamePhase;
  chanceCards: EventCard[];
  communityCards: EventCard[];
  tradeOffers: TradeOffer[];
  config: GameConfig;
  history: GameEvent[];
  winner: string | null;
}

// 保存的游戏接口
export interface SavedGame {
  version: string;
  timestamp: number;
  gameState: GameState;
  checksum: string;
}

// UI状态接口
export interface UIState {
  selectedPlayer: string | null;
  activeModal: string | null;
  notifications: Notification[];
  isAnimating: boolean;
  diceResult: number | null;
  showPropertyCard: string | null;
  showEventCard: EventCard | null;
  pendingChoice: {
    cardId: string;
    choices: Choice[];
  } | null;
}

// 应用状态接口
export interface AppState {
  game: GameState | null;
  ui: UIState;
  actions: {
    // 游戏管理
    createGame: (config: GameConfig) => void;
    joinGame: (gameId: string, playerName: string, character: Character) => void;
    startGame: () => void;
    
    // 回合操作
    rollDice: () => void;
    endTurn: () => void;
    
    // 地块操作
    buyProperty: (propertyId: string) => void;
    upgradeProperty: (propertyId: string) => void;
    mortgageProperty: (propertyId: string) => void;
    unmortgageProperty: (propertyId: string) => void;
    
    // 能力操作
    useAbility: (targetPlayerId?: string, targetPropertyId?: string) => void;
    
    // 交易操作
    createTrade: (offer: Omit<TradeOffer, 'id' | 'status' | 'timestamp'>) => void;
    respondToTrade: (tradeId: string, accept: boolean) => void;
    modifyTrade: (tradeId: string, changes: Partial<TradeOffer>) => void;
    
    // 破产处理
    sellProperty: (propertyId: string) => void;
    declareBankruptcy: () => void;
    
    // UI操作
    showNotification: (notification: Omit<Notification, 'id' | 'timestamp'>) => void;
    dismissNotification: (notificationId: string) => void;
    setActiveModal: (modalName: string | null) => void;
    selectPlayer: (playerId: string | null) => void;
    
    // 持久化
    saveGame: () => void;
    loadGame: (gameId: string) => void;
    clearSavedGame: () => void;
  };
}

// 错误类
export class GameError extends Error {
  constructor(
    public type: GameErrorType,
    message: string,
    public context?: unknown
  ) {
    super(message);
    this.name = 'GameError';
  }
}

// 棋盘主题接口
export interface BoardTheme {
  id: string;
  name: string;
  propertyNames: string[];
  colorGroups: {
    [key: string]: string; // color group name -> color hex
  };
  specialSpaceNames: {
    go: string;
    jail: string;
    freeParking: string;
    goToJail: string;
  };
}
